library(haven)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
categ <- read_dta(file = "ByCateg_v5.dta")

nutrients <- read_dta(file = "foodcode_nutrient_density.dta") 

categfull <- categ %>%
  filter(!is.na(price)) %>%
  filter(categ_name != "Uncat")

Initial Data Visualizations

ggplot(data = categfull, mapping = aes(x = price, y = densprot)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Protein on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = denssodi)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Sodium on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = dens_add_sugars)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Sugar on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

ggplot(data = categfull, mapping = aes(x = price, y = densfibe)) +
geom_point(aes(color = broadcateg_name, size = tspend), 
           alpha = 0.5) +
  labs(title = "Fiber on a Budget?", x = "Price/1000 kcal", y = "Protein/1000 kcal") +
  scale_size(guide = 'none') +
  scale_color_discrete(name = "Food Category")

First Plotly Iteration

Protein

# Legend
leg <- list(
  bordercolor = "#E2E2E2",
  borderwidth = 0.5)

# Protein 
prot <- plot_ly(
  categfull, x = ~price, y = ~densprot,
  # Hover text:
  text = ~paste("<b>Type:</b>", categ_name, "<br><b>Description:</b>", categ_des),
  color = ~broadcateg_name, size = ~tspend, opacity = 0.8
)

prot %>%
  layout(title = "Protein",
                     xaxis = list(title = "Price/1000kcal"), yaxis = list(title = "Protein(gm)/1000kcal"),
                     margin = list(l = 123), legend = leg) 

Fiber

# Legend
leg1 <- list(
  bordercolor = "#E2E2E2",
  borderwidth = 0.5)

# Protein 
fibe <- plot_ly(
  categfull, x = ~price, y = ~densfibe,
  # Hover text:
  text = ~paste("<b>Type:</b>", categ_name, "<br><b>Description:</b>", categ_des),
  color = ~broadcateg_name, size = ~tspend, opacity = 0.8
)

fibe %>%
  layout(title = "Fiber",
                     xaxis = list(title = "Price/1000kcal"), yaxis = list(title = "Fiber(gm)/1000kcal"),
                     margin = list(l = 123), legend = leg1) 

Plotly Visualization with Dropdown Feature

#Plot with Dropdown
prot2 <- plot_ly(
  data = categfull, x = ~price, y = ~densprot,
          color =  ~broadcateg_name, size = ~tspend,
          alpha = 0.7,
          width = 900,
          height = 500,
          hoverinfo = "text",
          text = ~paste0("<b>", categ_name, "</b><br>", "<b><i>Description: </b></i>", categ_des, "<br><b><i>Daily Spending/Capita: </b></i>", round(tspend, 3)),
    #ifelse(categfull$categ_name == categfull$categ_des, (~paste0("<b>", categ_name, "</b><br>", "<b><i>Description: </b></i>", categ_des, "<br><b><i>Total Spending/Capita: </b></i>", tspend)), (~paste0("<b>", categ_name, "</b><br>", "<br><b><i>Total Daily Spending: </b></i>", tspend))),         
          type = "scatter",
          mode = "markers"
          ) %>%
  layout(
    title = list(text = "<b>Finding Affordable Nutrient Sources</b>", x = .75),
    xaxis = list(title = "Price($)/1000kcal"),
    yaxis = list(title = ""),
    margin = list(b=150), ##right margin in pixels
    annotations = 
           list(x = 1.32, y = .4, #position of text adjust as needed
                text = "*Datapoint size reflects \nfood category's total \ndaily spending", 
                showarrow = F, xref='paper', yref='paper', 
                xanchor='right', yanchor='auto', xshift=0, yshift=0,
                font=list(size=12, color="grey")), 
    legend=list(title=list(text='<b> Broad Food Category </b>')),
       updatemenus = list(
        list(
          y = .55,
          x = -.05,
        type = "list",
        label = 'Category',
      buttons = list(
         list(method = "restyle",
              ##list(method = "update",
                  ##args = list("y", list(~densprot)),
          args = list(list(y = list(categfull$densprot))),
               #args = list("y", list(categfull$densprot)),
               label = "Protein(gm)/1000kcal"),
          list(
           ## method = "update",
           ##  args = list("y", list(~densfibe)),
            method = "restyle",
               args = list("y", list(categfull$densfibe)),
               label = "Fiber(gm)/1000kcal"),
          list( 
            ## method = "update",
            ## args = list("y", list(~dens_add_sugars)),
      method = "restyle",
               args = list("y", list(categfull$dens_add_sugars)),
               label = "Added Sugar(gm)/1000kcal"),
          list(
            ## method = "update",
            ## args = list("y", list(~denssodi)),
            method = "restyle",
              args = list("y", list(categfull$denssodi)),
               label = "Sodium(mg)/1000kcal")))))

prot2